<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>后台管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    {{--<meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />--}}
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="{{asset('xAdmin/css/font.css')}}">
    <link rel="stylesheet" href="{{asset('xAdmin/css/xadmin.css')}}">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="{{asset('xAdmin/lib/layui/layui.js')}}" charset="utf-8"></script>
    <script type="text/javascript" src="{{asset('xAdmin/js/xadmin.js')}}"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style type="text/css">
        .width-400 {
            width: 400px;
        }
    </style>
</head>

<body>
<div class="x-body">
    <form class="layui-form" id="edit-adv">
        {{csrf_field()}}
        <div class="layui-form-item">
            <label for="name" class="layui-form-label">
                <span class="x-red">*</span>广告名称
            </label>
            <div class="layui-input-block width-400">
                <input type="text" id="name" name="name" lay-verify="required"
                       autocomplete="off" value="{{$info['name']}}" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="img_url" class="layui-form-label">
                <span class="x-red">*</span>广告上传
            </label>
            <div class="layui-upload">
                <input type="hidden" name="img_url" id="img_url">
                <button type="button" class="layui-btn" id="test1">上传图片</button>
                <div class="layui-upload-list" style="margin-left: 110px">
                    <img class="layui-upload-img" id="demo1" src="{{asset('')}}/{{$info['img_url']}}">
                    <p id="demoText"></p>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="jump_url" class="layui-form-label">
                <span class="x-red">*</span>广告链接
            </label>
            <div class="layui-input-block width-400">
                <input type="text" id="jump_url" name="jump_url" lay-verify="url"
                       autocomplete="off" value="{{$info['jump_url']}}" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="order" class="layui-form-label">
                <span class="x-red">*</span>排序
            </label>
            <div class="layui-input-block width-400">
                <input type="text" id="order" name="order" lay-verify="required|number"
                       autocomplete="off" value="{{$info['order']}}" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label">
            </label>
            <button class="layui-btn" lay-filter="edit" lay-submit="">保存</button>
        </div>
    </form>
</div>
<script>
    // 普通图片上传
    layui.use('upload', function() {
        let $ = layui.jquery, upload = layui.upload;
        let uploadInst = upload.render({
            field: 'files',
            elem: '#test1',
            accept: 'images',
            url: '{{url("backstage/upload")}}',
            data: {
                "_token" : "{{csrf_token()}}"
            },
            before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            },
            done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg(res.msg);
                }
                $('#img_url').val(res.url);
                return layer.msg(res.msg);
                //上传成功
            },
            error: function () {
                //演示失败状态，并实现重传
                let demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
    });

    // 信息保存
    layui.use(['form','layer'], function(){
        let $ = layui.jquery;
        let form = layui.form, layer = layui.layer;
        //自定义验证规则
        form.verify({
            name: function(value){
                if(value.length < 4){
                    return '标题至少得4个字符啊';
                }
            },
        });
        //监听提交
        form.on('submit(edit)', function(){
            alert(111);
            $.ajax({
                url: '{{url("backstage/editAdv", ["id" => $info["id"]])}}',
                type: 'POST',
                dataType: 'json',
                data: $('#edit-adv').serialize(),
                success: function (result) {
                    if (result.status === 0) {
                        layer.msg(result.msg, {
                            icon: 1,
                            time: 2000
                        }, function () {
                            // 获得frame索引
                            let index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                        });
                    }
                },
                error: function () {
                    layer.msg('保存异常，请重试！');
                    return false;
                }
            });
            return false;
        });
    });
</script>

</body>

</html>
